<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名字" prop="eventName">
        <el-input
          v-model="queryParams.eventName"
          placeholder="请输入项目名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前参与人数" prop="nowNumber">
        <el-input
          v-model="queryParams.nowNumber"
          placeholder="请输入当前参与人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最大参与人数" prop="maxNumber">
        <el-input
          v-model="queryParams.maxNumber"
          placeholder="请输入最大参与人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运动会名称" prop="sportsMeetingId">
        <el-input
          v-model="queryParams.sportsMeetingName"
          placeholder="请输入运动会名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="eventList">
<!--      <el-table-column label="赛事项目ID" align="center" prop="eventId" />-->
      <el-table-column label="项目名字" align="center" prop="eventName" />
      <el-table-column label="当前参与人数" align="center" prop="nowNumber" />
      <el-table-column label="最大参与人数" align="center" prop="maxNumber" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="运动会名称" align="center" prop="sportsMeetingName" />
      <el-table-column label="报名开始时间" align="center" prop="applicaitonStart" />
      <el-table-column label="报名结束时间" align="center" prop="applicaitonEnd" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-order"
            @click="handleApplicate(scope.row)"
            v-hasPermi="['sports:event:enroll']"
          >报名</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 确认报名参加赛事项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <span style="font-size:36px;">您确定要报名参加该项目？</span>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEvent, getEvent,/* delEvent, addEvent,*/ enrollEvent } from "@/api/sports/enrollment";

export default {
  name: "Event",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 赛事项目表格数据
      eventList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        eventName: null,
        nowNumber: null,
        maxNumber: null,
        description: null,
        sportsMeetingId: null,
        sportsMeetingName: null,
        applicaitonStart: null,
        applicaitonEnd: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询赛事项目列表 */
    getList() {
      this.loading = true;
      listEvent(this.queryParams).then(response => {
        this.eventList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        eventId: null,
        eventName: null,
        nowNumber: null,
        maxNumber: null,
        description: null,
        sportsMeetingId: null,
        sportsMeetingName: null,
        applicaitonStart: null,
        applicaitonEnd: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleApplicate(row) {
      this.reset();
      const eventId = row.eventId || this.ids
      getEvent(eventId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.eventId != null) {
            enrollEvent(this.form).then(response => {
              this.$modal.msgSuccess("报名成功！请等待管理员审核。");
              this.open = false;
              this.getList();
            });
          }
          // else {
          //   addEvent(this.form).then(response => {
          //     this.$modal.msgSuccess("新增成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // }
        }
      });
    },
  }
};
</script>
